package com.example.lt.controller;

import com.example.lt.constant.MinioPicPathConstant;
import com.example.lt.constant.PicPathConstant;
import com.example.lt.service.MinioService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 小涛Tao
 * @Date: 2024/06/03/13:42
 * version 1.0 注释：
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class MinioController {

    private final MinioService minioService;

    @GetMapping("/minio/swiper/{fileName:.+}")
    public ResponseEntity<byte[]> getBanner(@PathVariable String fileName) throws Exception {
        log.info("获取轮播图：{}",fileName);
        return minioService.getImage(MinioPicPathConstant.BANNER_IMAGES_PATH,fileName);
    }

    /**
     * 从minio中获取用户头像
     * @param fileName 图片名称
     * @return
     * @throws Exception
     */
    @GetMapping("/minio/avatorImages/{fileName:.+}")
    public ResponseEntity<byte[]> getAvator(@PathVariable String fileName) throws Exception {
        log.info("获取用户头像：{}",fileName);
        return minioService.getImage(MinioPicPathConstant.AVATOR_IMAGES_PATH, fileName);
    }

    /**
     * 从minio中获取歌手图片
     * @param fileName 图片名称
     * @return
     * @throws Exception
     */
    @GetMapping("/minio/singerPic/{fileName:.+}")
    public ResponseEntity<byte[]> getSingerPic(@PathVariable String fileName) throws Exception {
        log.info("获取歌手图片：{}",fileName);
        return minioService.getImage(MinioPicPathConstant.SINGER_IMAGES_PATH, fileName);
    }

    /**
     * 从minio中获取歌曲图片
     * @param fileName 图片名称
     * @return
     * @throws Exception
     */
    @GetMapping("/minio/songPic/{fileName:.+}")
    public ResponseEntity<byte[]> getSongPic(@PathVariable String fileName) throws Exception {
        log.info("获取歌曲图片：{}",fileName);
        return minioService.getImage(MinioPicPathConstant.SONG_IMAGES_PATH, fileName);
    }

    /**
     * 从minio中获取歌单图片
     * @param fileName 图片名称
     * @return
     * @throws Exception
     */
    @GetMapping("/minio/songListPic/{fileName:.+}")
    public ResponseEntity<byte[]> getSongListPic(@PathVariable String fileName) throws Exception {
        log.info("获取歌单图片：{}",fileName);
        return minioService.getImage(MinioPicPathConstant.SONG_LIST_IMAGES_PATH, fileName);
    }

    /**
     * 下载歌曲
     * @param fileName
     * @param request
     * @return
     */
    @GetMapping("/download/{fileName}")
    public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
        log.info("下载歌曲：{}",fileName);
        return minioService.downloadFile(MinioPicPathConstant.SONG_PATH, fileName, request);
    }

    /**
     * 获取歌曲
     * @param fileName
     * @return
     */
    @GetMapping("/song/{fileName:.+}")
    public ResponseEntity<byte[]> getMusic(@PathVariable String fileName) {
        log.info("获取歌曲：{}",fileName);
        return minioService.getMusic(MinioPicPathConstant.SONG_PATH, fileName);
    }

}
